#include "nr_core.h"
#include "nr_device_nport.h"
#include "nr_device_apmd7xx.h"
#include "nr_nport_net_connector.h"
#include "nr_device_middle.h"

nr_device_nport::nr_device_nport()
{
	m_nport_thread = new nr_nport_net_connector();
	
	moveToThread(m_nport_thread);
}

nr_device_nport::~nr_device_nport()
{
	delete m_nport_thread;
}

nr_error_t nr_device_nport::init(const QVariantHash &args)
{
	return NR_ERROR_NOT_IMPLEMENTED;
}

nr_error_t nr_device_nport::start()
{
	if (m_status == NR_DEVICE_STATUS_STARTED)
		return NR_ERROR_NONE;
	
	//--lxm 2012.12.20 get mhost form xml
	
	nr_nport_net_connector &nport_thread_connector = *(nr_nport_net_connector*)m_nport_thread;

    Q_ASSERT(m_device_item);

#ifndef NR_NPORT_USE_SAMPLE_DATA

#define NR_DEVICE_NPORT_PROPERTY_ID_TCPIP 1

    nr_device_properties::iterator it = m_device_item->properties().find(NR_DEVICE_NPORT_PROPERTY_ID_TCPIP);
    Q_ASSERT(it != m_device_item->properties().end());


#define NR_DEVICE_NPORT_PROPERTY_ID_ADDRESS 1
#define NR_DEVICE_NPORT_PROPERTY_ID_PORT 2

    nr_device_property *host_prop = it.value()->children().find(NR_DEVICE_NPORT_PROPERTY_ID_ADDRESS).value();
    nr_device_property *port_prop = it.value()->children().find(NR_DEVICE_NPORT_PROPERTY_ID_PORT).value();

    nport_thread_connector.host() = host_prop->value().toString();
    nport_thread_connector.port() = (quint16)port_prop->value().toInt();

#endif

	//----------------------------


	// Start its parent
	nr_error_t status = nr_device::start();
	if (NR_FAILED(status))
		return status;

	// Start a new thread to get the data
	if (!m_nport_thread->isRunning())
		m_nport_thread->start();

	return NR_ERROR_NONE;
}

nr_error_t nr_device_nport::stop()
{
	// Should check whether all the child device has stoppped
	if (m_nport_thread->isRunning())
		m_nport_thread->quit();

	return nr_device::stop();
}

nr_error_t nr_device_nport::set_property(nr_id_t prop_id, const QVariant &in_prop)
{
	return NR_ERROR_NOT_IMPLEMENTED;
}

nr_error_t nr_device_nport::get_property(nr_id_t prop_id, QVariant &out_prop)
{
	return NR_ERROR_NOT_IMPLEMENTED;
}

nr_error_t nr_device_nport::perform_action(nr_id_t action_id, const QVariantHash &in_params, QVariantHash &out_params)
{
	return NR_ERROR_NOT_IMPLEMENTED;
}

void nr_device_nport::add_child_device(nr_device *child_device)
{
	//first invoke  base class's add_child_device
	nr_device::add_child_device(child_device);

	//set the 
	nr_device_middle *middle_device=(nr_device_middle *)child_device;
	middle_device->set_connector((nr_nport_net_connector*)m_nport_thread);

}
void nr_device_nport::put_data(nr_data *data)
{
	Q_ASSERT(data);
	Q_ASSERT(this->device_item());

	// Compare nport device id
	if (this->device_item()->id() == data->device_id())
	{
		this->send_out_data(data);
		return ;
	}

	// Compare the sub-device device id
	foreach(nr_device* device, m_child_device_list)
	{
		Q_ASSERT(device->device_item());

		if (device->device_item()->id() == data->device_id())
		{
			device->send_out_data(data);
			break;
		}
	}
}
